package com.zsk.okhttp;

import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;

/**
 * 
 * @author keke
 * @date 2020/04/23
 */
public class HttpFactory {

    /**
     * 是否忽略SSL证书
     * 
     * @param ignoreSSL
     * @return
     * @date 2020/04/16 14:49:38
     */

    public static OkHttpClient newOkHttpClient(boolean ignoreSsl) {
        if (!ignoreSsl) {
            return new OkHttpClient();
        }

        OkHttpClient.Builder builder = newHttpBuilder();
        SSLSocketFactory sslFactory = SslSocketClient.getSslSocketFactory();
        X509TrustManager x509TrustManager = SslSocketClient.getTrustManager()[0];
        builder.sslSocketFactory(sslFactory, x509TrustManager).hostnameVerifier(SslSocketClient.getHostnameVerifier());

        return builder.build();
    }

    public static OkHttpClient.Builder newHttpBuilder() {
        return new OkHttpClient.Builder();
    }

}
